package com.demo.money.controller.costdetails;

import com.demo.money.kit.JSONResult;
import com.demo.money.kit.example.CostdetailsExample;
import com.demo.money.model.Costdetails;
import com.demo.money.model.Type;
import com.demo.money.model.User;
import com.demo.money.service.CostdetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class JSONCostdetails {
    @Autowired
    private CostdetailsService costdetailsService;
    /**
     * 条件查询
     *
     * @param userId             所属用户 - 等于
     * @param startTime          开始时间 - 大等于
     * @param endTime            结束时间 - 小等于
     * @param startMoney         起始金额 - 大等于
     * @param endMoney           最大金额 - 小等于
     * @param largeclass         类别 - 等于
     * @param subclass           支出平台 - LIKE
     * @param tradename          商品名称 - LIKE
     * @param satisfactiondegree 满意度 - 等于
     * @param note               备注 - LIKE
     * @return 跳转页面
     */
    @GetMapping({"/jsonSelectByExampleCostdetails"})
    public JSONResult selectByExample(HttpServletRequest request,
                                      @RequestParam("startTime") String startTime,
                                      @RequestParam("endTime") String endTime,
                                      @RequestParam("startMoney") String startMoney,
                                      @RequestParam("endMoney") String endMoney,
                                      @RequestParam("largeclass") String largeclass,
                                      @RequestParam("subclass") String subclass,
                                      @RequestParam("tradename") String tradename,
                                      @RequestParam("satisfactiondegree") String satisfactiondegree,
                                      @RequestParam("note") String note) {

        //model.addAttribute("userId", userId).addAttribute("startTime", startTime).addAttribute("endTime", endTime).addAttribute("startMoney", startMoney).addAttribute("endMoney", endMoney).addAttribute("largeclass", largeclass).addAttribute("subclass", subclass).addAttribute("tradename", tradename).addAttribute("satisfactiondegree", satisfactiondegree).addAttribute("note", note);
        User user = (User) request.getSession().getAttribute("user");
        String userId = user.getId().toString();
        List<Costdetails> list = costdetailsService.selectByZhongHe(userId, startTime, endTime, startMoney, endMoney, largeclass, subclass, tradename, satisfactiondegree, note);


        return JSONResult.ok(list);
    }

    /**
     * 查询支出明细页面
     * <p>
     * 需查询全部支出数据
     * selectByExample
     * @return 响应页面地址
     */
    @GetMapping("/jsonCostdetailsIndexList")
    public JSONResult jsonCostdetailsIndexList(HttpServletRequest request){
        CostdetailsExample costdetailsExample = new CostdetailsExample();

        CostdetailsExample.Criteria criteria = costdetailsExample.createCriteria();

        criteria.andValidEqualTo(1);

        User user = (User) request.getSession().getAttribute("user");

        criteria.andUserIdEqualTo(8);//user.getId()

        costdetailsExample.setOrderByClause("costdetails.time_transaction DESC");

        List<Costdetails> list = costdetailsService.selectByExample(costdetailsExample);

        return JSONResult.ok(list);

    }
}
